﻿using System;
using System.Data;
using System.Data.SqlClient;
using Gean.Data.Common;

namespace Pansoft.ORM
{
    public class SqlServerHelper : DatabaseHelper
    {
        private readonly string _ConnectionString;
        private SqlConnection _Connection;
        private SqlCommand _Command;
        public SqlServerHelper(string connectionString)
        {
            _ConnectionString = connectionString;
        }

        #region Overrides of DatabaseHelper

        /// <summary>
        /// 连接字符串
        /// </summary>
        public override string ConnectionString
        {
            get { return _ConnectionString; }
        }

        public override IDbConnection Connection
        {
            get { return _Connection ?? (_Connection = new SqlConnection(ConnectionString)); }
        }

        public override IDbCommand Command
        {
            get { return _Command ?? (_Command = new SqlCommand
                {
                    Connection = _Connection ?? (_Connection = new SqlConnection(ConnectionString)),
                }); }
        }

        /// <summary>
        /// 在每次操作前是否需要关闭连接。一般针对文件型数据库(Aceess,SQLite)不需要关闭连接。
        /// </summary>
        /// <value>
        ///   <c>true</c> if [need connection close]; otherwise, <c>false</c>.
        /// </value>
        public override bool NeedConnectionClose
        {
            get { return true; }
        }

        public override IDbDataAdapter GetAdapter(IDbCommand command)
        {
            return new SqlDataAdapter((SqlCommand) command);
        }

        #endregion
    }
}